/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Brain;

import javax.sound.sampled.*;
import java.lang.Exception;
import java.util.concurrent.Callable;

/**
 *
 * @author Dan dotcom
 */

public class CAlerta extends Thread {

  public static float SAMPLE_RATE = 6000f;
  
  public void run() {
      try
      {
          for(;;)
          {
              tono(500, 1000, 9.0);
          }
      }catch (LineUnavailableException ex) {
          Thread t = new Thread();
          t.getUncaughtExceptionHandler().uncaughtException(t, ex);
          
      }
  }
  

  public void tono(int hz, int msecs, double vol)
      throws LineUnavailableException 
  {
    byte[] buf = new byte[1];
    AudioFormat af = 
        new AudioFormat(
            SAMPLE_RATE, 
            8,           
            1,           
            true,        
            false);      
    SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
    sdl.open(af);
    sdl.start();
    for (int i=0; i < msecs*8; i++) {
      double angle = i / (SAMPLE_RATE / hz) * 2.0 * Math.PI;
      buf[0] = (byte)(Math.sin(angle) * 127.0 * vol);
      sdl.write(buf,0,1);
    }
    sdl.drain();
    sdl.stop();
    sdl.close();
  }
}
